package com.javajun.controller;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javajun.entity.Claxx;
import com.javajun.entity.MainCourse;
import com.javajun.entity.Manager;
import com.javajun.entity.Student;
import com.javajun.service.MainCourseService;
import com.javajun.service.ManagerService;

/**
 * @ClassName: MainCourseController
 * @Description: 主修课程的控制器
 * @author: xlm
 * @date: 2018年10月13日
 */
@Controller
@RequestMapping(value="/admin/maincourse")
public class MainCourseController {

	@Autowired
	private MainCourseService mainCourseService;
	
	@Autowired
	private ManagerService managerService;
	
	// 显示所有课程信息
	@RequestMapping(value="/manage")
	public String Manage(ModelMap map, MainCourse entity) {
		List<MainCourse> models = mainCourseService.findAll(BuildSpec(entity));
		// List<Manager> managers = managerService.findAll();
		// 所有班主任
		// map.put("managers", managers);
		// 搜索条件
		map.put("searchs", entity);
		// 查询结果
		map.put("models", models);
		/*for (MainCourse mainCourse : models) {
			System.out.println("选择这门课总人数：" + mainCourse.getStudentTotal());
			for (Claxx claxx : mainCourse.getClaxxs()) {
				System.out.println("选择这门课的班级有：" + claxx.getName());
			}
			for (Student student : mainCourse.getStudents()) {
				System.out.println("选择这门课的学生有：" + student.getName());
			}
		}*/
		
		return "maincourse/manage";
	}
	
	// 修改页面
	@RequestMapping(value="/edit")
	public String Edit(ModelMap map, Integer id) {
		MainCourse model = new MainCourse();
		if (id != null) {
			model = mainCourseService.findById(id);
		}
		map.put("model", model);
		return "maincourse/edit";
	}
	
	// 保存方法
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String Save(ModelMap map, MainCourse entity) {
		MainCourse mainCourse = new MainCourse();
		if (entity.getId() != null) {
			mainCourse = mainCourseService.findById(entity.getId());
		}
		if (entity.getName() != null) {
			mainCourse.setName(entity.getName());
		}
		if (entity.getCredit() != null) {
			mainCourse.setCredit(entity.getCredit());
		}
		if (entity.getHourse() != null) {
			mainCourse.setHourse(entity.getHourse());
		}
		mainCourseService.save(mainCourse);
		return "redirect:manage";
	}
	
	// 新增页面
	@RequestMapping(value="/add")
	public String Add(ModelMap map) {
		return "maincourse/add";
	}
	
	// 删除方法
	@RequestMapping(value="delete")
	public String Delete(ModelMap map, Integer id) {
		if (id != null) {
			MainCourse entity = new MainCourse();
			entity = mainCourseService.findById(id);
			mainCourseService.delete(entity);
		}
		return "redirect:manage";
	}
	
	// 动态安全查询
	public Specification<MainCourse> BuildSpec(MainCourse entity) {
		Specification<MainCourse> spec = new Specification<MainCourse>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<MainCourse> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				predicates.add(cb.greaterThan(root.get("id"), 0));
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		return spec;
	}
}
